(function () {
    angular.module('app')
        .factory('httpService', httpService);

    /**
     * httpServices 服务,未验证是否可用
     */
    httpService.$inject = ['$http', '$q', '$window'];
    function httpService($http, $q, $window) {

        return {
            "eapGet": sendMessage__get,
            "eapPost": sendMessage__post,
            "sendMessage": sendMessage,
            "httpGet": httpGet,
            "httpPost": httpPost,
            "sessionTimeOut": sessionTimeOut
        };

        /**
         * 以get的方式请求eap接口,使用$q迟绑定。
         *
         * @param url   get请求的url
         * @param errorMessage  设置通讯异常后的错误信息
         * @returns delay.promise
         */
        function sendMessage__get(url, errorMessage) {
            var delay = $q.defer();
            $http.get(url).success(function (data) {

                if (checkErrorCode(data.errorCode)) {
                    delay.resolve(data);
                } else {

                    if (data.errorCode === "PLT0013" || data.errorCode === 'PLT0014') {
                        sessionTimeOut();
                    }

                    delay.reject(data);
                }

            }).error(function (e) {
                delay.reject(e);
                throw new Error(errorMessage);
            });

            return delay.promise;
        }

        /**
         *  以post的方式请求eap接口,使用$q迟绑定。
         *
         *
         * @param url  请求的url
         * @param params    请求的参数
         * @param errorMessage  设置通讯异常后的错误信息
         * @returns delay.promise
         */
        function sendMessage__post(url, params, errorMessage) {
            var delay = $q.defer();

            $http.post(url, params).success(function (data) {

                if (checkErrorCode(data.errorCode)) {

                    delay.resolve(data);
                } else {

                    if (data.errorCode === "PLT0013" || data.errorCode === 'PLT0014') {
                        sessionTimeOut();
                    }

                    delay.reject(data);
                }

            }).error(function (e) {
                delay.reject(e);
                throw new Error(errorMessage);
            });

            return delay.promise;
        }

        /**
         *      已最原始的angular $http函数方式，
         *  去请求eap接口。
         *
         * @param options  请求的options
         * @param errorMessage  设置通讯异常后的错误信息
         * @returns delay.promise
         */
        function sendMessage(options, errorMessage) {
            var delay = $q.defer();
            $http(options).success(function (data) {

                delay.resolve(data);

            }).error(function (e) {

                delay.reject(e);
                throw new Error(errorMessage);
            });

            return delay.promise;
        }

        function httpGet(url) {
            var delay = $q.defer();
            $http.get(url).success(function (data) {
                delay.resolve(data);
            }).error(function (e) {
                delay.reject(e);
                throw new Error('服务器通信错误');
            });

            return delay.promise;
        }

        function httpPost(url, params) {
            var localTokenId = $window.localStorage.getItem('user.tokenId') || '1426168189510ahehKS6IBRpmOmoDEMoUIZzJF0gd';
            if (!params) {
                var params = {};
                params.tokenId = localTokenId;
                params.token = localTokenId;
            } else if (params && !params.tokenId && localTokenId) {
                params.tokenId = localTokenId;
                params.token = localTokenId;
            }
            var delay = $q.defer();
            $http.post(url, params).success(function (data, status, headers, config) {
                delay.resolve(data);
            }).error(function (data, status, headers, config) {
                delay.reject(data);
                throw new Error('服务器通信错误');
            });

            return delay.promise;
        }


        function checkErrorCode(errorCode) {
            return ( angular.isUndefined(errorCode) ) ||
                ( errorCode === "000000" ) ||
                ( errorCode === '0000' );
        }

        function sessionTimeOut() {
            //清楚localStorage记录
            $window.localStorage.removeItem("user.custName");
            $window.localStorage.removeItem("user.SID");
            $window.localStorage.removeItem("user.custLabel");
            $window.localStorage.removeItem("user.workFlg");


            //关闭dialog
            if (typeof paymentDialog !== "undefined" &&
                angular.isFunction(paymentDialog.destroy)) {
                paymentDialog.destroy();
            }

            var pages = mgNavigator.getPages();

            if ((pages.length - 1) > 0) {
                //返回主页
                for (var i = ( pages.length - 1 ); i > 1; i--) {
                    pages[i].destroy();
                }
                index_tabbar.setActiveTab(0);
                mgNavigator.popPage();
            }

        }

    }

})();